package xyz.quanguanjia.masterboy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import xyz.quanguanjia.masterboy.MBDApplication;
import xyz.quanguanjia.masterboy.MBDGroupDetailModel;
import xyz.quanguanjia.masterboy.MBDHostManager;
import xyz.quanguanjia.masterboy.R;
import xyz.quanguanjia.masterboy.util.StringUtils;

public class HomeListAdapter extends RecyclerView.Adapter {
    private List<MBDGroupDetailModel> groupDetailList;

    public HomeListAdapter(List<MBDGroupDetailModel> gd) {
        this.groupDetailList = gd;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建列表项视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cell_home, parent, false);
        return new MBDGroupDetailViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // 绑定数据到列表项视图
        MBDGroupDetailModel detail = this.groupDetailList.get(position);
        ((MBDGroupDetailViewHolder) holder).bind(detail);
    }

    @Override
    public int getItemCount() {
        return this.groupDetailList.size();
    }

    public static class MBDGroupDetailViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTextView, timeTv, ageTextView, nameTv;
        private ImageView groupLogo, headImage, likeIv;

        public MBDGroupDetailViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.text_group_name);
            nameTv = itemView.findViewById(R.id.name_tv);
            timeTv = itemView.findViewById(R.id.time_tv);
            ageTextView = itemView.findViewById(R.id.text_group_desc);
            groupLogo = itemView.findViewById(R.id.image_group_logo);
            headImage = itemView.findViewById(R.id.head_image);
            likeIv = itemView.findViewById(R.id.like_iv);
        }

        public void bind(MBDGroupDetailModel detail) {
            // 设置列表项视图的数据
            nameTextView.setText(detail.getGroupName());
            if (!StringUtils.isNull(detail.getGroupDesc())) {
                ageTextView.setText(detail.getGroupDesc().replace("\n", " "));
            } else {
                ageTextView.setText(detail.getGroupDesc());
            }
            nameTv.setText(detail.getGroupPoster());
            timeTv.setText(getTime(detail.getCreateTime()));
            String imageUrl;
            if (detail.getGroupLogo().indexOf("http") > -1) {
                imageUrl = detail.getGroupLogo();
            } else {
                imageUrl = MBDHostManager.IMAGE_HOST + detail.getGroupLogo();
            }
            String avatarUrl = MBDHostManager.IMAGE_HOST + detail.getAvatarUrl();
            int cornerRadius = 10; // 圆角半径，单位为像素
            RequestOptions options = new RequestOptions()
                    .transform(new CenterCrop(), new RoundedCorners(cornerRadius));
            Glide.with(MBDApplication.getContext())
                    .load(imageUrl)
                    .apply(options)
                    .into(groupLogo);
            Glide.with(MBDApplication.getContext())
                    .load(avatarUrl)
                    .apply(RequestOptions.circleCropTransform())
                    .placeholder(R.drawable.circle)
                    .error(R.drawable.circle)
                    .into(headImage);
            likeIv.setAlpha(!StringUtils.isNull(detail.isLike) && detail.isLike.equals("1") ? 1f : 0.5f);
        }
    }

    private static String getTime(String publishTimeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date publishTime = sdf.parse(publishTimeStr);
            // 获取当前时间
            Date now = new Date();
            // 计算时间差（毫秒）
            long diffInMillis = now.getTime() - publishTime.getTime();
            // 转换为小时、天和月
            long diffInHours = diffInMillis / (1000 * 60 * 60);
            long diffInDays = diffInHours / 24;
            long diffInMonths = diffInDays / 30;
            if (diffInMonths > 0) {
                return publishTimeStr.substring(0, 10);
            }
            if (diffInMonths <= 0 && diffInDays > 0) {
                return diffInDays + "天前";
            }
            if (diffInMonths <= 0 && diffInDays <= 0 && diffInHours > 0) {
                return diffInHours + "小时前";
            }
            return publishTimeStr.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
            return publishTimeStr.substring(0, 10);
        }
    }
}
